package com.rc.evcharger.protocol.ylevcharger.mq;


import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.rc.evcharger.common.config.RabbitListenerConstant;
import com.rc.evcharger.protocol.ylevcharger.action.YlAsyncTask;
import com.rc.evcharger.protocol.ylevcharger.action.YlBizAction;
import com.rc.evcharger.protocol.ylevcharger.service.YlCommandService;
import com.rc.evcharger.service.log.ChargedProcessLogService;
import com.rc.evcharger.vo.charge.PriceReq;
import com.rc.evcharger.vo.log.ChargedProcessLog;
import com.rc.mutievcharger.dto.ylevcharger.terminal.YlTerminalMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 启停充电（平台）指令监听器
 * Created by sven on 2019/3/4
 */
@Component
public class YlDomain1Receiver{
    private static Logger logger = LogManager.getLogger(YlDomain1Receiver.class);

    @Autowired
    private YlBizAction ylBizAction;

    @Autowired
    private YlCommandService ylCommandService;

    @Autowired
    private YlAsyncTask asyncTask;

    @Autowired
    private ChargedProcessLogService chargedProcessLogService;

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_01_QUEUE})
    @RabbitHandler
    public void process01(String msg) {
        logger.info("(CMD=1)后台服务器下发充电桩整型工作参数DOMAIN1_YL_CONTROLL_01_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            ylBizAction.timing(terminalMessage);
        } catch (Exception e) {
            logger.error("(CMD=1)后台服务器下发充电桩整型工作参数DOMAIN1_YL_CONTROLL_01_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_03_QUEUE})
    @RabbitHandler
    public void process03(String msg) {
        logger.info("(CMD=3)后台服务器下发充电桩字符型工作参数(校时)DOMAIN1_YL_CONTROLL_03_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            ylBizAction.timing(terminalMessage);
        } catch (Exception e) {
            logger.error("(CMD=3)后台服务器下发充电桩字符型工作参数(校时)DOMAIN1_YL_CONTROLL_03_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_4F_04_QUEUE})
    @RabbitHandler
    public void process4F_04(String msg) {
        logger.info("(CMD=1103)后台服务器下发电价DOMAIN1_YL_CONTROLL_4F_04_QUEUE 收到了:{},channel:{}", msg);

        PriceReq priceReq;
        try {
            priceReq = JSON.parseObject(msg, PriceReq.class);
            ylCommandService.sendPrice(priceReq.getElecPileId());
        } catch (Exception e) {
            logger.error("(CMD=1103)后台服务器下发电价DOMAIN1_YL_CONTROLL_4F_04_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_6A_QUEUE}, containerFactory = "customContainerFactory")
    @RabbitHandler
    public void process6A(String msg) {
        //String msg = new String(message.getBody(), "UTF-8");
        logger.info("登录指令DOMAIN1_YL_CONTROLL_6A_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            ylBizAction.login(terminalMessage);
        } catch (Exception e) {
            logger.error("登录指令DOMAIN1_YL_CONTROLL_6A_QUEUE 处理失败", e);
            return;
        }
    }


    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_66_QUEUE}, containerFactory = "customContainerFactory")
    @RabbitHandler
    public void process66(String msg, Channel channel) {
        logger.info("心跳指令DOMAIN1_YL_CONTROLL_66_QUEUE  1收到了:{}", msg);
        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            asyncTask.hearBeat(terminalMessage);
        } catch (Exception e) {
            logger.warn("心跳指令DOMAIN1_YL_CONTROLL_66_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_68_QUEUE}, containerFactory = "customContainerFactory")
    @RabbitHandler
    public void process68(String msg) {
        //String msg = new String(message.getBody(), "UTF-8");
        logger.info("充电桩状态信息包请求DOMAIN1_YL_CONTROLL_68_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            asyncTask.chargeReport(terminalMessage);
            //stdBizAction.chargeReport(terminalMessage);
        } catch (Exception e) {
            logger.error("充电桩状态信息包请求DOMAIN1_YL_CONTROLL_68_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_CA_QUEUE})
    @RabbitHandler
    public void processCA(String msg) {
        //String msg = new String(message.getBody(), "UTF-8");
        logger.info("结算指令DOMAIN1_YL_CONTROLL_CA_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);

            ylBizAction.transactonsReport(terminalMessage);
        } catch (Exception e) {
            logger.error("DOMAIN1_YL_CONTROLL_CA_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_08_QUEUE})
    @RabbitHandler
    public void process08(String msg) {
        logger.info("启动应答DOMAIN1_YL_CONTROLL_08_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            ylBizAction.startResp(terminalMessage);
        } catch (Exception e) {
            logger.error("启动应答DOMAIN1_YL_CONTROLL_08_QUEUE 处理失败", e);
            return;
        }
    }

    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN1_YL_CONTROLL_06_QUEUE})
    @RabbitHandler
    public void process06(String msg) {
        logger.info("停止应答DOMAIN1_YL_CONTROLL_06_QUEUE 收到了:{},channel:{}", msg);

        YlTerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, YlTerminalMessage.class);
            ylBizAction.stopResp(terminalMessage);
        } catch (Exception e) {
            logger.error("停止应答DOMAIN1_YL_CONTROLL_06_QUEUE 处理失败", e);
            return;
        }
    }


    @RabbitListener(queues = {RabbitListenerConstant.DOMAIN_LOG_CHARGED_PROCESS_QUEUE})
    @RabbitHandler
    public void processChargeLog(String msg) {
        logger.info("充电日志processChargeLog 收到了:{},channel:{}", msg);
        try {
            ChargedProcessLog log = new Gson().fromJson(msg, ChargedProcessLog.class);
            //新增的MONGODB LOG
            chargedProcessLogService.addChargedProcessLog(log);
        } catch (Exception e) {
            logger.error("充电日志processChargeLog 处理失败", e);
            return;
        }
    }
}
